{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "finished-canvas",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "sporting-accounting",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = np.loadtxt(r'..\\class05_demension\\sales_data.txt', delimiter=',', dtype=int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "urban-fundamental",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[104, 177, 189, 156, 121, 126],\n",
       "       [146, 131, 179, 193, 190, 125],\n",
       "       [152, 106, 109, 168, 174, 143],\n",
       "       [178, 130, 189, 164, 133, 156]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "recovered-trigger",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([['F', 'T', 'T', 'T', 'F', 'F'],\n",
       "       ['F', 'F', 'T', 'T', 'T', 'F'],\n",
       "       ['T', 'F', 'F', 'T', 'T', 'F'],\n",
       "       ['T', 'F', 'T', 'T', 'F', 'T']], dtype='<U1')"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 按条件填充\n",
    "np.where(data > 150, 'T', 'F')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "meaning-borough",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1040, 17700, 18900, 15600,  1210,  1260],\n",
       "       [ 1460,  1310, 17900, 19300, 19000,  1250],\n",
       "       [15200,  1060,  1090, 16800, 17400,  1430],\n",
       "       [17800,  1300, 18900, 16400,  1330, 15600]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 原理见doc\n",
    "np.where(data > 150, data * 100, data * 10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "popular-cambridge",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 2, 3, 4, 0, 0],\n",
       "       [0, 0, 3, 4, 5, 0],\n",
       "       [1, 0, 0, 4, 5, 0],\n",
       "       [1, 0, 3, 4, 0, 6]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 广播机制，原理见doc\n",
    "np.where(data > 150, [1, 2, 3, 4, 5, 6], 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "clear-amino",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([['啥也不是', '2月之星', '3月之星', '4月之星', '啥也不是', '啥也不是'],\n",
       "       ['啥也不是', '啥也不是', '3月之星', '4月之星', '5月之星', '啥也不是'],\n",
       "       ['1月之星', '啥也不是', '啥也不是', '4月之星', '5月之星', '啥也不是'],\n",
       "       ['1月之星', '啥也不是', '3月之星', '4月之星', '啥也不是', '6月之星']], dtype='<U4')"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.where(data > 150, ['1月之星','2月之星','3月之星','4月之星','5月之星','6月之星'], '啥也不是')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "genetic-philip",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([['1月之耻', '2月之星', '3月之星', '4月之星', '5月之耻', '6月之耻'],\n",
       "       ['1月之耻', '2月之耻', '3月之星', '4月之星', '5月之星', '6月之耻'],\n",
       "       ['1月之星', '2月之耻', '3月之耻', '4月之星', '5月之星', '6月之耻'],\n",
       "       ['1月之星', '2月之耻', '3月之星', '4月之星', '5月之耻', '6月之星']], dtype='<U4')"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.where(data > 150, ['1月之星','2月之星','3月之星','4月之星','5月之星','6月之星'], \n",
    "         ['1月之耻','2月之耻','3月之耻','4月之耻','5月之耻','6月之耻'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "handy-northwest",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[104,   1,   1,   1, 121, 126],\n",
       "       [146, 131,   1,   1,   1, 125],\n",
       "       [  1, 106, 109,   1,   1, 143],\n",
       "       [  1, 130,   1,   1, 133,   1]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.where(data > 150, 1, data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "outstanding-cuisine",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3], dtype=int64),\n",
       " array([1, 2, 3, 2, 3, 4, 0, 3, 4, 0, 2, 3, 5], dtype=int64))"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 大于150的元素坐标\n",
    "np.where(data > 150)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "comparative-colonial",
   "metadata": {},
   "outputs": [],
   "source": [
    "s = np.where(data > 150)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "controlling-millennium",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3], dtype=int64)"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "placed-sending",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 2, 3, 4, 0, 3, 4, 0, 2, 3, 5], dtype=int64)"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "ideal-cathedral",
   "metadata": {},
   "outputs": [],
   "source": [
    "list01 = list(zip(s[0], s[1]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "typical-notion",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(0, 1),\n",
       " (0, 2),\n",
       " (0, 3),\n",
       " (1, 2),\n",
       " (1, 3),\n",
       " (1, 4),\n",
       " (2, 0),\n",
       " (2, 3),\n",
       " (2, 4),\n",
       " (3, 0),\n",
       " (3, 2),\n",
       " (3, 3),\n",
       " (3, 5)]"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list01"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "therapeutic-walnut",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(0, 1)"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list01[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "empirical-routine",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list01[0][1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "hundred-donna",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "177"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[0][1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "italic-dimension",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "177"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[list01[0][0]][list01[0][1]]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
